package com.mlz.mlzmall.ui.activity.act.personal;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.JsonRequest;
import com.google.gson.Gson;
import com.mlz.mlzmall.R;
import com.mlz.mlzmall.bean.StatusBean;
import com.mlz.mlzmall.ui.activity.act.base.TopTitleView;
import com.mlz.mlzmall.ui.activity.act.main.MyApplication;
import com.mlz.mlzmall.ui.activity.act.personal.NetworkRequest.VolleyBaseActivity;
import com.mlz.mlzmall.ui.activity.act.personal.userLogin.ChangePasswordAgain;
import com.mlz.mlzmall.ui.utils.mlz.ConfigInfo;
import com.mlz.mlzmall.ui.utils.mlz.URL;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * Created by yhf on 2016/5/18.
 */
public class ChangePwdActivity extends VolleyBaseActivity {

    private EditText et_old_pwd;//旧密码
    private EditText et_new_pwd;//新密码
    private EditText et_reNew_pwd;//重新输入新密码
    private Button btn_save;//保存
    private String password;
    private String newPassword;
    private String confirmPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.change_pwd);//修改密码界面
        initView();
        setViewContent();
    }
    private void setViewContent() {
        TopTitleView loginTopTitle = (TopTitleView) findViewById(R.id.login_top_title);
        loginTopTitle.setLeftButtonContent(null, R.mipmap.pic_2_1_btn_back, null);
        loginTopTitle.setTitleButtonContent("修改密码", null, null);
        loginTopTitle.setOnLeftButtonClickListener(new TopTitleView.OnLeftButtonClickListener() {
            @Override
            public void onLeftBtnClick(View button) {
                finish();
                ChangePwdActivity.this.overridePendingTransition(R.anim.right_in,R.anim.right_out);
            }
        });
    }
    private void initView() {
        et_old_pwd = (EditText) findViewById(R.id.et_old_pwd);
        et_new_pwd = (EditText) findViewById(R.id.et_new_pwd);
        et_reNew_pwd = (EditText) findViewById(R.id.et_reNew_pwd);
        btn_save = (Button) findViewById(R.id.btn_save);
        btn_save.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_save:
                password = et_old_pwd.getText().toString().trim();
                newPassword = et_new_pwd.getText().toString().trim();
                confirmPassword = et_reNew_pwd.getText().toString().trim();
                //提示密码修改成功，返回上一层
                if (password.length() == 0 || newPassword.length() == 0 || confirmPassword.length() == 0) {
                    showToast("请把信息填写完整");
                }else if (newPassword.length() < 8) {
                    showToast("新密码不能少于8个字符");
                } else if (confirmPassword.length() < 8) {
                    showToast("重复密码不能少于8个字符");
                } else if (newPassword.equals(password)) {
                    showToast("新密码不能和旧密码相同");
                } else if (!newPassword.equals(confirmPassword)) {
                    showToast("两次密码不一致，请重新输入");
                } else {
                    postChangePwd(password, newPassword, confirmPassword);
                }
                break;
        }
    }

    private void postChangePwd(String password, String newPassword, String confirmPassword) {
        tag = "rPost";
        Map<String, String> paraml = new HashMap<>();
        paraml.put("password", isNull(password));
        paraml.put("newPassword", isNull(newPassword));
        paraml.put("confirmPassword", isNull(confirmPassword));

        JSONObject jsonObject = new JSONObject(paraml);
        JsonRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, URL.CUSTOMER.EDIT_PWD, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.e(tag, jsonObject.toString());
                Gson gson = new Gson();
                StatusBean sb = gson.fromJson(jsonObject.toString(), StatusBean.class);
                if (sb.getStatus() == 0) {
                    showToast(sb.getMsg());
                    finish();
                    ChangePwdActivity.this.overridePendingTransition(R.anim.right_in, R.anim.right_out);
                } else {
                    showToast("原密码不正确");
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(tag, error.getMessage(), error);
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<>();
                map.put(ConfigInfo.CHARSET, ConfigInfo.UTF8);
                map.put(ConfigInfo.COOKIE, ConfigInfo.COOKIE_KEY + sessionId);
                map.put(ConfigInfo.TYPE, ConfigInfo.TYPE_KEY);
                return map;
            }
        };
        //设置该请求的标签
        jsonRequest.setTag(tag);
        //将请求添加到请求队列中
        ((MyApplication) getApplication()).getRequestQueue().add(jsonRequest);
    }

}
